package sau.diyizu.media.menu;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import sau.diyizu.media.R;

import java.util.List;

/**
 @author JYD
 @version 1.0
 @since 2020/12/16 20:18 */
public class MenuDialog extends Dialog {
    public static final String TAG = "MenuDialog";

    private List<MenuItem> menuItemList;
    private final MenuAdapter adapter = new MenuAdapter();

    public MenuDialog(@NonNull Context context) {
        super(context, R.style.BaseDialogTheme);
        setContentView(R.layout.dialog_menu);
        RecyclerView content_rv = findViewById(R.id.content_rv);
        content_rv.setAdapter(adapter);

        adapter.setItemListener((view, pos, type) -> {
            MenuItem item = menuItemList.get(pos);
            item.listener.onSelected(item, pos);
            dismiss();
        });
    }

    public void setMenuItemList(List<MenuItem> items) {
        this.menuItemList = items;
        adapter.setDataAndNotify(items);
    }

    public void show() {
        if (menuItemList == null || menuItemList.size() == 0) {
            // 菜单弹窗无法正常显示，选项列表不能为空
            Log.w(TAG, "show: The menu pop-up window cannot be displayed normally," +
                    " and the option list cannot be empty");
        }
        super.show();
    }
}
